﻿using System;
using System.Data.SqlClient;

using Fuse8.DomainFramework.Domain;

namespace Fuse8.DomainFramework.DataAccess.SqlDatabase
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SqlUnitOfWork<T> : IDbUnitOfWork, IUnitOfWork<T> where T : class, IEntityObject
    {
        #region Fields

        private bool _isDisposed = false;

        #endregion  

        #region Properties

        /// <summary>
        /// 
        /// </summary>
        public SqlConnection Connection { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public SqlTransaction Transaction { get; set; }

        #endregion

        #region Constructor

        /// <summary>
        /// 
        /// </summary>
        public SqlUnitOfWork()
        { }

        #endregion
        
        #region IDisposable Members

        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            Dispose(true);

            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!_isDisposed)
            {
                if (disposing)
                {
                    if (Connection != null)
                    {
                        if (Transaction != null)
                        {
                            Transaction.Dispose();
                        }

                        Connection.Close();
                    }
                }
            }

            _isDisposed = true;
        }

        #endregion
    }
}
